package com.huang.controller;

import com.huang.pojo.Menu;
import com.huang.service.MenuService;
import com.huang.vo.PageResult;
import com.huang.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/menu")
@ResponseBody
public class MenuController {
    @Resource
    private MenuService menuService;

    @RequestMapping(value = "/isExistMenu/{menuName}", method = RequestMethod.GET)
    public Result isExistMenu(@PathVariable("menuName") String name) {
        Menu menu = menuService.isExistName(name);
        Result result;
        if (menu != null) {
            result = new Result(200, "资源名称已存在，请重新输入", "success", menu);
        } else result = new Result(200, "", "fail", null);
        return result;
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result insert(Menu menu) {
        menu.setIsDel(0);
        int insert = menuService.insert(menu);
        Result result;
        if (insert == 1) {
            result = new Result(200, null, "success", null);
        } else result = new Result(200, null, "fail", null);
        return result;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result list(String m_name, Integer pageIndex, Integer pageSize) {
        PageResult list = menuService.list(m_name, pageIndex, pageSize);
        Result result = new Result(200, "查询成功", null, list);
        return result;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") Integer id) {
        Menu menu = menuService.findById(id);
        Result result;
        if (menu == null) result = new Result(200, "error", "没有该信息", null);
        else result = new Result(200, "success", "查询成功", menu);
        return result;
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public Result update(Menu menu) {
        int update = menuService.update(menu);
        Result result;
        if (update != 1) result = new Result(200, "error", "更新失败，请重写输入", null);
        else result = new Result(200, "success", "更新成功", null);
        return result;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id") Integer id) {
        int delete = menuService.delete(id);
        Result result;
        if (delete != 1) result = new Result(200, "error", "删除失败", null);
        else result = new Result(200, "success", "删除成功", null);
        return result;
    }

    @RequestMapping(value = "/batch/{ids}", method = RequestMethod.DELETE)
    public Result deleteByBatch(@PathVariable("ids") String ids) {
        String[] split = ids.split(",");
        int delete = menuService.delete(split);
        Result result;
        if (delete < 1) result = new Result(200, "error", "删除失败", null);
        else result = new Result(200, "success", "删除成功", null);
        return result;
    }

    // 查询所有menu
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public Result getAll() {
        List<Menu> menus = menuService.getAll();
        Result result;
        result = new Result(200, "success", "删除失败", menus);
        return result;
    }
}
